package cn.change.basic.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 16252
 */
@Configuration
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //添加配置信息
        CorsConfiguration configuration = new CorsConfiguration();
        //白名单
        configuration.addAllowedOrigin("http://bugtracker.itsource.cn");
        configuration.addAllowedOrigin("http://localhost:8081");
        configuration.addAllowedOrigin("http://127.0.0.1:8080");

        configuration.addAllowedOrigin("http://localhost:8080");
        configuration.addAllowedOrigin("http://127.0.0.1:80");
        configuration.addAllowedOrigin("http://bugtracker.itsource.cn:80");
        configuration.addAllowedOrigin("http://localhost:80");
        configuration.addAllowedOrigin("http://127.0.0.1");
        configuration.addAllowedOrigin("http://localhost");
        //允许cookie
        configuration.setAllowCredentials(true);
        //允许的请求方式
        configuration.addAllowedMethod("POST");
        configuration.addAllowedMethod("OPTIONS");
        configuration.addAllowedMethod("HEAD");
        configuration.addAllowedMethod("GET");
        configuration.addAllowedMethod("PUT");
        configuration.addAllowedMethod("DELETE");
        configuration.addAllowedMethod("PATCH");
        configuration.addAllowedMethod("OPTIONS");
        //允许的头信息
        configuration.addAllowedHeader("*");
        //2.添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", configuration);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}
